<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getTypes">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="types" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="sn" label="编号" width="120" sortable>
			</el-table-column>
			<el-table-column prop="name" label="类型名称" width="100"  sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" :current-page="page" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>


		<!--编辑界面-->
		<el-dialog :title=title :visible.sync="typeFormVisible" :close-on-click-modal="false">
			<el-form :model="typeForm" label-width="80px" :rules="typeFormRules" ref="typeForm">
				<el-form-item label="编号" prop="sn">
					<el-input v-model="typeForm.sn" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="名称" prop="name">
					<el-input v-model="typeForm.name" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="typeFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					name: ''
				},
				title:"",
				types: [],
				total: 0,
				page: 1,
				pageSize: 5,
				listLoading: false,
				sels: [],//列表选中列

				typeFormVisible: false,//编辑界面是否显示
				editLoading: false,
				typeFormRules: {
					name: [
						{required: true, message: '请输入类型名称', trigger: 'blur'}
					]
				},
				//编辑界面数据
				typeForm: {
					id: null,
					sn:'',
					name: '',
				},
				details:[]
			}
		},
		methods: {
			//性别显示转换
			formatSex: function (row, column) {
				return row.state == 1 ? '启用' : row.state == 0 ? '禁用' : '未知状态';
			},
			handleCurrentChange(val) {
				this.page = val;
				this.getTypes();
			},
			//获取部门列表
			getTypes() {
				let para = {
					localPage: this.page,
					name: this.filters.name
				};
				this.listLoading = true;
				this.$http.post("/type", para)
						.then(result => {
							this.types = result.data.rows;
							this.total = result.data.total;
							this.listLoading = false;
						}).catch(result => {
					alert("系统错误！")
				})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '温馨提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let url = "/type/" + row.id;
					this.$http.delete(url).then(result => {
						if (result) {
							//关闭确认框
							this.listLoading = false;
							//提示信息
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							//刷新表格
							if (this.total > this.pageSize)
								if ((this.total - 1) % this.pageSize == 0)
									this.page = this.total / this.pageSize;
							this.getTypes();
						} else {
							this.listLoading = false;
							//提示信息
							this.$message({
								message: '删除失败',
								type: 'success'
							});
						}
					}).catch(result => {
						alert("系统错误！");
					})
					/*removeDepartment(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getTypes();
					});*/
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.title="编辑";
				this.getDeptTree();
				this.typeForm = Object.assign({}, row);
				//上级部门回显
				let dirPath = row.dirPath; // /2/5/11
				if(dirPath){
					let tmp = [];
					if(dirPath){
						let dirPaths =  dirPath.trim().split("/"); //['','2','5','11']
						console.log(dirPaths)
						for(let i=1;i<dirPaths.length-1;i++){
							tmp[i-1] = parseInt(dirPaths[i]);
						}
					}

					this.typeForm.parent = tmp; //根据dirpath构造数组
				}
				this.typeFormVisible = true;
			},
			//显示新增界面
			handleAdd: function () {
				this.title="添加";
				//每次点击都会去后天后去数据,加载部门结构
				this.getDeptTree();
				this.typeForm = {
					name: '',
					sn:''
				};
				this.typeFormVisible = true;
			},
			//保存
			editSubmit: function () {
				this.$refs.typeForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = Object.assign({}, this.typeForm);
							/*
								因为typeForm.parent的值为数组[父部门id，子部门id....]，而传到后台的值应该是对象
								所以在这里将数组最后一个值取出，即是该部门的上级部门
							*/
							if (para.parent){
								let arr = para.parent;
								para.parent={'id':arr[arr.length-1]};
							}
							this.$http.put("/type",para).then((res) => {
								//关闭忙等框（）
								this.editLoading = false;
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['typeForm'].resetFields();
								this.typeFormVisible = false;
								if (this.total%this.pageSize==0){
									this.total=this.total+1;
									this.page=this.total/this.pageSize+1;
								}
								this.getTypes();
							}).catch(res=>{
								this.editLoading = false;
								this.typeFormVisible = false;
								alert("系统错误！")
							})
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();

					this.$http.patch("/type", ids).then(res => {
						this.listLoading = false;
						if (res.data.message) {
							this.$message({
								message: '删除成功',
								type: 'success'
							});
						} else {
							this.$message({
								message: res.data.message,
								type: 'success'
							});
						}
						this.page = 1;
						this.getDetails();
					}).catch(() => {
						alert("系统错误！")
					});
				})
			},
			getDetails(){
				this.$http.get("/detail").then(result=>{
					if (result)
					this.details=result.data;
				})
			},
		},
		mounted() {
				this.getTypes();
				this.getDetails();
		}

	}
</script>

<style scoped>

</style>